package com.example.qxfw.shfx.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.example.qxfw.common.entity.xt.SysConfig;
import com.example.qxfw.shfx.Mapper.YjfxshYzdispparaMapper;
import com.example.qxfw.shfx.Mapper.YjfxshYzdispparaRadMapper;
import com.example.qxfw.shfx.Service.YjcsService;
import com.example.qxfw.shfx.vo.YjcsJsxwx;
import com.example.qxfw.shfx.vo.YjcsVoxwx;
import com.example.qxfw.shfx.entity.YjfxshYzdisppara;
import com.example.qxfw.shfx.entity.YjfxshYzdispparaRad;
import com.example.qxfw.ybzz.mapper.SysConfigMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class YjcsServiceImpl implements YjcsService {
    @Autowired
    private YjfxshYzdispparaMapper yjfxshYzdispparaMapper;
    @Autowired
    private YjfxshYzdispparaRadMapper yjfxshYzdispparaRadMapper;
    @Autowired
    private SysConfigMapper sysConfigMapper;


    @Override
    public YjfxshYzdisppara settingParametersInsert(double value, int color) {
        YjfxshYzdisppara yjfxshYzdisppara = new YjfxshYzdisppara();
        yjfxshYzdisppara.setFactorindex(-1);
        yjfxshYzdisppara.setValue(value);
        yjfxshYzdisppara.setColour(color);
        int insert = yjfxshYzdispparaMapper.insert(yjfxshYzdisppara);
        if (insert > 0) {
            return yjfxshYzdisppara;
        }else {
            return null;
        }
    }

    @Override
    public YjfxshYzdisppara settingParametersUpdate(YjfxshYzdisppara yjfxshYzdisppara) {
        int update = yjfxshYzdispparaMapper.update(yjfxshYzdisppara, new UpdateWrapper<YjfxshYzdisppara>()
                .eq("id", yjfxshYzdisppara.getId())
                .eq("factorindex", yjfxshYzdisppara.getFactorindex()));
        if (update > 0){
            return yjfxshYzdisppara;
        }else {
            return null;
        }
    }

    @Override
    public int settingParametersDelete(int id) {
        return yjfxshYzdispparaMapper.deleteById(id);
    }

    @Override
    public List<YjfxshYzdisppara> settingParameterSelect() {
        return yjfxshYzdispparaMapper.selectList(new LambdaQueryWrapper<YjfxshYzdisppara>()
        .eq(YjfxshYzdisppara::getFactorindex, -1));
    }

    @Override
    public String savedqybpara(YjcsJsxwx ybrainpara1) {
        String dzpara = ybrainpara1.getDzpara();
        String ybrainpara = ybrainpara1.getYbrainpara();
        String skrainpara = ybrainpara1.getSkrainpara();
        String sk3 = ybrainpara1.getSk3();
        String sk6 = ybrainpara1.getSk6();
        String sk9 = ybrainpara1.getSk9();
        dzpara = dzpara == null ? "0" : dzpara;
        skrainpara = skrainpara == null ? "0" : skrainpara;
        ybrainpara = ybrainpara == null ? "0" : ybrainpara;
        sk3 = sk3 == null ? "0" : sk3;
        sk6 = sk6 == null ? "0" : sk6;
        sk9 = sk9 == null ? "0" : sk9;

        SysConfig SH_dzpara = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_dzpara")
                .eq(SysConfig::getParamtype, "地质因子值"));
        if (SH_dzpara.getParamvalue() == null || !SH_dzpara.getParamvalue().equals(dzpara)){
            sysConfigMapper.updateByNameAndTypeOfSysConfig(dzpara, "SH_dzpara", "地质因子值");
            System.out.println("地质因子值修改完成！");
        }else {
            System.out.println("地质因子值未发生变化！");
        }

        SysConfig SH_skrainpara = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_skrainpara")
                .eq(SysConfig::getParamtype, "实况雨量因子值"));
        if (SH_skrainpara.getParamvalue() == null || !SH_skrainpara.getParamvalue().equals(skrainpara)){
            sysConfigMapper.updateByNameAndTypeOfSysConfig(skrainpara, "SH_skrainpara", "实况雨量因子值");
            System.out.println("实况雨量因子值修改完成！");
        }else {
            System.out.println("实况雨量因子值未发生变化！");
        }

        SysConfig SH_ybrainpara = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_ybrainpara")
                .eq(SysConfig::getParamtype, "预报雨量因子值"));
        if (SH_ybrainpara.getParamvalue() == null || !SH_ybrainpara.getParamvalue().equals(ybrainpara)){
            sysConfigMapper.updateByNameAndTypeOfSysConfig(ybrainpara, "SH_ybrainpara", "预报雨量因子值");
            System.out.println("预报雨量因子值修改完成！");
        }else {
            System.out.println("预报雨量因子值未发生变化！");
        }

        SysConfig SH_skRain123 = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_skRain123")
                .eq(SysConfig::getParamtype, "实况雨量分子值"));
        if (SH_skRain123.getParamvalue() == null || !SH_skRain123.getParamvalue().equals(sk3)){
            sysConfigMapper.updateByNameAndTypeOfSysConfig(sk3, "SH_skRain123", "实况雨量分子值");
            System.out.println("实况雨量分子值123修改完成！");
        }else {
            System.out.println("实况雨量分子值123未发生变化！");
        }
        SysConfig SH_skRain456 = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_skRain456")
                .eq(SysConfig::getParamtype, "实况雨量分子值"));
        if (SH_skRain456.getParamvalue() == null || !SH_skRain456.getParamvalue().equals(sk6)){
            sysConfigMapper.updateByNameAndTypeOfSysConfig(sk6, "SH_skRain456", "实况雨量分子值");
            System.out.println("实况雨量分子值456修改完成！");
        }else {
            System.out.println("实况雨量分子值456未发生变化！");
        }
        SysConfig SH_skRain789 = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_skRain789")
                .eq(SysConfig::getParamtype, "实况雨量分子值"));
        if (SH_skRain789.getParamvalue() == null || !SH_skRain789.getParamvalue().equals(sk9)){
            sysConfigMapper.updateByNameAndTypeOfSysConfig(sk6, "SH_skRain789", "实况雨量分子值");
            System.out.println("实况雨量分子值789修改完成！");
        }else {
            System.out.println("实况雨量分子值789未发生变化！");
        }
        return "修改完成！！！";
    }
    @Override
    public YjcsVoxwx settingParametersInsertLT1() {
        SysConfig SH_dzpara = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_dzpara")
                .eq(SysConfig::getParamtype, "地质因子值"));
        SysConfig SH_skrainpara = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_skrainpara")
                .eq(SysConfig::getParamtype, "实况雨量因子值"));
        SysConfig SH_ybrainpara = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_ybrainpara")
                .eq(SysConfig::getParamtype, "预报雨量因子值"));
        SysConfig SH_skRain123 = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_skRain123")
                .eq(SysConfig::getParamtype, "实况雨量分子值"));
        SysConfig SH_skRain456 = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_skRain456")
                .eq(SysConfig::getParamtype, "实况雨量分子值"));
        SysConfig SH_skRain789 = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_skRain789")
                .eq(SysConfig::getParamtype, "实况雨量分子值"));
        return YjcsVoxwx.builder()
                .SHDzparaRad(SH_dzpara.getParamvalue())
                .SHSkrainparaRad(SH_skrainpara.getParamvalue())
                .SHYbrainparaRad(SH_ybrainpara.getParamvalue())
                .SHSkRainRad123(SH_skRain123.getParamvalue())
                .SHSkRainRad456(SH_skRain456.getParamvalue())
                .SHSkRainRad789(SH_skRain789.getParamvalue())
                .build();
    }
    //-----------------------------------------------------------------------------------------------------------------------
    @Override
    public YjfxshYzdispparaRad settingParametersInsertDL(double value, int color) {
        YjfxshYzdispparaRad yjfxshYzdispparaRad = new YjfxshYzdispparaRad();
        yjfxshYzdispparaRad.setFactorindex(-2);
        yjfxshYzdispparaRad.setValue(value);
        yjfxshYzdispparaRad.setColour(color);
        int insert = yjfxshYzdispparaRadMapper.insert(yjfxshYzdispparaRad);
        if (insert > 0) {
            return yjfxshYzdispparaRad;
        }else {
            return null;
        }

    }

    @Override
    public YjfxshYzdispparaRad settingParametersUpdateDL(YjfxshYzdispparaRad yjfxshYzdispparaRad) {
        int update = yjfxshYzdispparaRadMapper.update(yjfxshYzdispparaRad, new UpdateWrapper<YjfxshYzdispparaRad>()
                .eq("id", yjfxshYzdispparaRad.getId())
                .eq("factorindex", yjfxshYzdispparaRad.getFactorindex()));
        if (update > 0) {
            return yjfxshYzdispparaRad;
        }else {
            return null;
        }
    }

    @Override
    public int settingParametersDeleteDL(int id) {
        return yjfxshYzdispparaRadMapper.deleteById(id);
    }

    @Override
    public List<YjfxshYzdispparaRad> settingParameterSelectDL() {
        return yjfxshYzdispparaRadMapper.selectList(new LambdaQueryWrapper<YjfxshYzdispparaRad>()
                .eq(YjfxshYzdispparaRad::getFactorindex, -2));
    }

    @Override
    public String savedqybparaRd(YjcsJsxwx ybrainpara1) {
        String dzpara = ybrainpara1.getDzpara();
        String ybrainpara = ybrainpara1.getYbrainpara();
        String skrainpara = ybrainpara1.getSkrainpara();
        String sk3 = ybrainpara1.getSk3();
        String sk6 = ybrainpara1.getSk6();
        String sk9 = ybrainpara1.getSk9();
        dzpara = dzpara == null ? "0" : dzpara;
        skrainpara = skrainpara == null ? "0" : skrainpara;
        ybrainpara = ybrainpara == null ? "0" : ybrainpara;
        sk3 = sk3 == null ? "0" : sk3;
        sk6 = sk6 == null ? "0" : sk6;
        sk9 = sk9 == null ? "0" : sk9;

        SysConfig SH_dzpara = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_dzparaRad")
                .eq(SysConfig::getParamtype, "地质因子值Rad"));
        if (SH_dzpara.getParamvalue() == null || !SH_dzpara.getParamvalue().equals(dzpara)){
            sysConfigMapper.updateByNameAndTypeOfSysConfig(dzpara, "SH_dzparaRad", "地质因子值Rad");
            System.out.println("地质因子Rad值修改完成！");
        }else {
            System.out.println("地质因子Rad值未发生变化！");
        }

        SysConfig SH_skrainpara = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_skrainparaRad")
                .eq(SysConfig::getParamtype, "实况雨量因子值Rad"));
        if (SH_skrainpara.getParamvalue() == null || !SH_skrainpara.getParamvalue().equals(skrainpara)){
            sysConfigMapper.updateByNameAndTypeOfSysConfig(skrainpara, "SH_skrainparaRad", "实况雨量因子值Rad");
            System.out.println("实况雨量因子值Rad修改完成！");
        }else {
            System.out.println("实况雨量因子值Rad未发生变化！");
        }

        SysConfig SH_ybrainpara = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_ybrainparaRad")
                .eq(SysConfig::getParamtype, "预报雨量因子值Rad"));
        if (SH_ybrainpara.getParamvalue() == null || !SH_ybrainpara.getParamvalue().equals(ybrainpara)){
            sysConfigMapper.updateByNameAndTypeOfSysConfig(ybrainpara, "SH_ybrainparaRad", "预报雨量因子值Rad");
            System.out.println("预报雨量因子值Rad修改完成！");
        }else {
            System.out.println("预报雨量因子值Rad未发生变化！");
        }

        SysConfig SH_skRain123 = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_skRainRad123")
                .eq(SysConfig::getParamtype, "实况雨量分子值Rad"));
        if (SH_skRain123.getParamvalue() == null || !SH_skRain123.getParamvalue().equals(sk3)){
            sysConfigMapper.updateByNameAndTypeOfSysConfig(sk3, "SH_skRainRad123", "实况雨量分子值Rad");
            System.out.println("实况雨量分子值Rad123修改完成！");
        }else {
            System.out.println("实况雨量分子值Rad123未发生变化！");
        }
        SysConfig SH_skRain456 = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_skRainRad456")
                .eq(SysConfig::getParamtype, "实况雨量分子值Rad"));
        if (SH_skRain456.getParamvalue() == null || !SH_skRain456.getParamvalue().equals(sk6)){
            sysConfigMapper.updateByNameAndTypeOfSysConfig(sk6, "SH_skRainRad456", "实况雨量分子值Rad");
            System.out.println("实况雨量分子值Rad456修改完成！");
        }else {
            System.out.println("实况雨量分子值Rad456未发生变化！");
        }
        SysConfig SH_skRain789 = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_skRainRad789")
                .eq(SysConfig::getParamtype, "实况雨量分子值Rad"));
        if (SH_skRain789.getParamvalue() == null || !SH_skRain789.getParamvalue().equals(sk9)){
            sysConfigMapper.updateByNameAndTypeOfSysConfig(sk9, "SH_skRainRad789", "实况雨量分子值Rad");
            System.out.println("实况雨量分子值Rad789修改完成！");
        }else {
            System.out.println("实况雨量分子值Rad789未发生变化！");
        }
        return "修改完成！！！";
    }

    @Override
    public YjcsVoxwx settingParametersInsertLT() {
        SysConfig SH_dzpara = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_dzparaRad")
                .eq(SysConfig::getParamtype, "地质因子值Rad"));
        SysConfig SH_skrainpara = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_skrainparaRad")
                .eq(SysConfig::getParamtype, "实况雨量因子值Rad"));
        SysConfig SH_ybrainpara = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_ybrainparaRad")
                .eq(SysConfig::getParamtype, "预报雨量因子值Rad"));
        SysConfig SH_skRain123 = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_skRainRad123")
                .eq(SysConfig::getParamtype, "实况雨量分子值Rad"));
        SysConfig SH_skRain456 = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_skRainRad456")
                .eq(SysConfig::getParamtype, "实况雨量分子值Rad"));
        SysConfig SH_skRain789 = sysConfigMapper.selectOne(new LambdaQueryWrapper<SysConfig>()
                .eq(SysConfig::getParamname, "SH_skRainRad789")
                .eq(SysConfig::getParamtype, "实况雨量分子值Rad"));
        return YjcsVoxwx.builder()
                .SHDzparaRad(SH_dzpara.getParamvalue())
                .SHSkrainparaRad(SH_skrainpara.getParamvalue())
                .SHYbrainparaRad(SH_ybrainpara.getParamvalue())
                .SHSkRainRad123(SH_skRain123.getParamvalue())
                .SHSkRainRad456(SH_skRain456.getParamvalue())
                .SHSkRainRad789(SH_skRain789.getParamvalue())
                .build();
    }



}
